<template>
  <el-container class="top">
    <!-- 头部区域 -->
    <el-header>
      <div class="nav-logo">
        <a href="#">
          <img src="../assets/logo.png" alt="logo" />
          <span>电商后台管理系统</span>
        </a>
      </div>
      <el-button type="info" @click="out">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="toggleCollapse" @click="toggle_collapse">|||</div>
        <el-menu
          background-color="#323744"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
          :default-active="$route.path"
          :collapse="iscollapse"
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="i + ''" v-for="(item, i) in menusList" :key="i">
            <template slot="title">
              <i :class="icon[i]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- vue-router以index为path跳转，
            如果不以斜杠开头的，都会被当成普通的字符串拼到后面-->
            <el-menu-item
              v-for="secondItem in item.children"
              :key="secondItem.id"
              :index="'/' + secondItem.path + ''"
            >
              <template slot="title">
                <i class="el-icon-s-order"></i>
                <span slot="title">{{ secondItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单列表数据
      menusList: {},
      //菜单图标列表
      icon: {
        '0': 'iconfont iconyonghuguanli',
        '1': 'iconfont iconpermission',
        '2': 'iconfont iconguanli',
        '3': 'iconfont iconlogooo_huabanfuben',
        '4': 'iconfont icontongji'
      },
      // 控制侧边连是否折叠
      iscollapse: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    out() {
      // 清空有效token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error('获取菜单列表失败')
      }
      this.menusList = res.data
    },
    // 控制侧边栏的水平折叠与否
    toggle_collapse() {
      this.iscollapse = !this.iscollapse
    }
  }
}
</script>

<style lang="less" scoped>
.top {
  height: 100%;
}
.nav-logo a {
  text-decoration: none;
  color: #eaedf2;
  //align-items: center;属性定义flex子项元素在该行的侧轴（纵轴）上居中放置
  display: flex;
  align-items: center;
  img {
    width: 50px;
    height: 50px;
  }
}
// 头部区域样式
.el-header {
  background-color: #373c41;
  font-size: 20px;
  //flex弹性布局，加上两边等宽的分布设置
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
}
// 侧边栏区域样式
.el-aside {
  background-color: #323744;
  .el-menu {
    border-right: none;
  }
}
//主题内容区域
.el-main {
  background-color: #eaedf2;
}

.iconfont {
  margin-right: 10px;
}
.toggleCollapse {
  background-color: #485166;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2em;
  color: #fff;
  cursor: pointer;
}
</style>
